package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.GroupUser;
import com.grantz.function.bbs.core.inter.GroupUserService;
import com.grantz.model.bbs.GroupUserModel;
import com.grantz.model.bbs.GroupUserSearch;
import com.grantz.models.base.element.page.ElementPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件名称： com.grantz.function.bbs.core.controller.GroupUserController.java</br>
 * 初始作者： WenBin</br>
 * 创建日期： 2020年2月13日</br>
 * 功能说明： 群组用户关系<br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 *  Copyright (c) 2010-2011 .All rights reserved.<br/>
 */
@RestController
@RequestMapping("/api/v1/groupuser")
@Api(value="群组用户",tags={"群组用户接口"})
public class GroupUserController {

	@Resource
	private GroupUserService  groupUserService;

	// 获取群组用户
	@ApiOperation(value = "获取分页信息")
	@GetMapping
	public ResponseEntity<ElementPage<GroupUserModel>>  get( GroupUserSearch query) throws Exception{
		ElementPage<GroupUserModel> element =  groupUserService.queryElementPage(query);
		return new ResponseEntity<ElementPage<GroupUserModel>>(element ,HttpStatus.CREATED);
	}

	// 添加信息
	@ApiOperation(value = "添加信息")
	@PutMapping
	public ResponseEntity<?>  add( GroupUserModel  model) throws Exception{
		//  添加信息
		GroupUser  entity = pop(new  GroupUser(), model);
		groupUserService.save(entity);
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	// 添加信息
	@ApiOperation(value = "更新群组信息")
	@PostMapping
	public ResponseEntity<?>  update( GroupUserModel  model) throws Exception{
		Optional<GroupUser>   option = groupUserService.findOneById(model.getId());
		//  添加信息
		if(option.isPresent()) {
			GroupUser  entity = pop(option.get(), model);
			groupUserService.save(entity);
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	private  GroupUser   pop(GroupUser entity ,GroupUserModel  model) {
		entity.setGroupId(model.getGroupId());
		entity.setSsoId(model.getSsoId());
		entity.setGroupRole(model.getGroupRole());
		entity.setUserLevel(model.getUserLevel());
		return entity;
	}
}
